package com.withoutwords.ui;

import com.withoutwords.engine.MyInfo;
import com.withoutwords.engine.comminication.CommunicationManager;
import com.withoutwords.engine.comminication.IServerResultListener;
import com.withoutwords.engine.comminication.ServerRequests;
import com.withoutwords.engine.settings.Settings;
import com.withoutwords.engine.social.ISocialNetworkController;
import com.withoutwords.engine.social.SocialEngine;
import com.withoutwords.engine.social.SocialNetwork;
import com.withoutwords.engine.social.listeners.ICurrentUserInfoListener;
import com.withoutwords.R;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class LoginActivity extends BaseActivity implements OnClickListener,ICurrentUserInfoListener,IServerResultListener {
	ISocialNetworkController fbController ;
	Handler uiHandler;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.login);

		Button btn = (Button) findViewById(R.id.btnLoginFb);
		btn.setOnClickListener(this);
		uiHandler = new Handler();

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnLoginFb: {
			fbController= SocialEngine.getSocialEngine().getSocialNetworkController(SocialNetwork.Facebook);
			fbController.requestLogin(this);
			break;
		}
		}
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		boolean bSuccess = fbController.onLoginCallback(requestCode, resultCode, data);
		if(bSuccess)
		{
			fbController.getCurrentUserInfo(this);
		}
	//	finish();
	}

	@Override
	public void onError(String Error) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onCurrentUserInfoRecieved(MyInfo me) {
		
		Settings sett = Settings.getInstance();
		if (sett.getMyInfo() == null) {
			sett.setMyInfo("", me.getSocialUser(), me.getEmail(), false);

			Bundle data = new Bundle();
			data.putString("Email", "mona.shokrof@gmail.com");// me.getEmail());
			data.putString("socialID", me.getSocialUser().getSocialID());
			data.putString("platFormId", "1");
			data.putString("tokenId", sett.getPushToken());

			CommunicationManager.getCommnuicationManager().request(
					ServerRequests.registerUser, data, this);

		}
	}

	@Override
	public void onRequestSucceed(ServerRequests serverRequest, Bundle response) {
		if(serverRequest == ServerRequests.registerUser)
		{
			String userId = response.getString(CommunicationManager.userIdCode);
			Settings sett = Settings.getInstance();
			MyInfo me  = sett.getMyInfo();
			me.setUserId(userId);
			sett.setMyInfo(me, true);
			startActivity(new Intent(getApplicationContext(),WithoutWordsActivity.class));
			finish();
		}
		
	}

	@Override
	public void onRequestFailed(ServerRequests serverRequest, final String error) {
		uiHandler.post(new Runnable() {
			
			@Override
			public void run() {
				Toast.makeText(LoginActivity.this, error, Toast.LENGTH_LONG).show();
				finish();				
			}
		});
		
		
	}
}
